ALTER PROC SP_SAVE_BASE_PRODUCT_BIN_MAP_DETAILS

@BASEPRODUCT_ID 	INT,
@BIN_NUMBER		VARCHAR(50),
@IS_CHECKED		BIT,
@DOWNSIZE_BIN_NUMBER	VARCHAR(50)

AS
BEGIN
	DECLARE @BIN_PROCESSSTEP_ID BIGINT

	--SELECT @BIN_PROCESSSTEP_ID = ProcessStep_ID FROM fn_get_process_flow_backward_by_id ( @PROCESSSTEP_ID ) WHERE Process_Type_Name = 'SORT'
	
	SELECT @BIN_PROCESSSTEP_ID = PS.ProcessStep_ID FROM PRODUCT P, PROCESS_STEP PS WHERE P.PROCESSSTEP_ID = PS.PROCESSSTEP_ID AND PS_TYPE_NAME = 'SORT' AND P.P_PARENT_PRODUCT_ID = @BASEPRODUCT_ID

	IF ( ( SELECT COUNT(*) FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASEPRODUCT_ID AND Bin_Number = @BIN_NUMBER ) = 0 )
	BEGIN
		INSERT INTO MRP_Binning_Mapping VALUES ( @BASEPRODUCT_ID, @BIN_NUMBER, CURRENT_TIMESTAMP, @DOWNSIZE_BIN_NUMBER )
		/*IF( @IS_CHECKED = 0 )
		BEGIN
			INSERT INTO MRP_Binning_Mapping VALUES ( @BASEPRODUCT_ID, @BIN_NUMBER, CURRENT_TIMESTAMP, @DOWNSIZE_BIN_NUMBER )
		END
		ELSE
		BEGIN
			UPDATE MRP_Binning_Mapping SET Bin_Number = '' WHERE Base_Product_ID = -1
		END*/
	END
	ELSE
	BEGIN
		--UPDATE Assembly_Bin_Mapping SET Bin_Number = @BIN_NUMBER
		--DELETE FROM Assembly_Bin_Mapping WHERE Assm_ProcessStep_ID = @PROCESSSTEP_ID AND Bin_Number = @BIN_NUMBER
		
		UPDATE MRP_Binning_Mapping SET Downsize_Bin_Number = @DOWNSIZE_BIN_NUMBER WHERE Base_Product_ID = @BASEPRODUCT_ID AND Bin_Number = @BIN_NUMBER
		
		/*IF( @IS_CHECKED = 1 )
		BEGIN
			DELETE FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASEPRODUCT_ID AND Bin_Number = @BIN_NUMBER
		END
		ELSE
		BEGIN
			UPDATE MRP_Binning_Mapping SET Downsize_Bin_Number = @DOWNSIZE_BIN_NUMBER WHERE Base_Product_ID = @BASEPRODUCT_ID AND Bin_Number = @BIN_NUMBER
		END*/
	END				
END